import { createPool } from 'mysql'

const DB_CONFIG = {
  host: 'localhost',
  user: 'keke',
  password: '123123',
  port: '3306',
  database: 'starrivercup'
}
const pool = createPool(DB_CONFIG)
/**
 * 
 * @param {sql语句} sql 
 * @param {参数} sqlArr 
 * @param {是不是只有一条?} only 
 * @returns 
 */
export const sqlMain = (str, arr, only) =>{
  return new Promise((resolve, reject)=>{
    pool.getConnection((err, con)=>{
      if(err) {
          console.log("数据库连接失败")
          reject(err)
      }else{
        con.query(str, arr, (error, result, filed)=>{
          if(error)
            reject(error)
          else
            resolve(only?result[0]:result)
        })
      }
      pool.releaseConnection(con)
    })
  })
}


/* export const sqlMain = (sql,sqlArr,only) => {
  return new Promise((success,error)=>{
    db.sqlConnect(sql,sqlArr,(err,data)=>{
      if(err) {
        console.log(err)
        error("这个真的是数据库异常哦")
      }
      else{
        //RowDataPacket????
        data = JSON.parse(JSON.stringify(data))
        success(only?data[0]:data)
      }
    })
  })
}
 */